package cn.com.spic.demo.impl;

import cn.com.spic.demo.api.ActionProjectService;
import cn.com.spic.demo.dao.ActionProDAO;
import cn.com.spic.demo.dao.OrganizationDAO;
import cn.com.spic.demo.dao.ProgressReportDAO;
import cn.com.spic.demo.dto.*;
import cn.com.spic.demo.po.*;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@RequiredArgsConstructor
public class ActionProjectServiceImpl implements ActionProjectService {

    //    @Autowired
//    private BenchMarkDAO benchMarkDAO;
    @Autowired
    private OrganizationDAO organizationDAO;

    //    private final ActionProService actionProService;
    @Autowired
    private ProgressReportDAO progressReportDAO;

    @Autowired
    private ActionProDAO actionProDAO;

    /**
     * 获取行动项
     *
     * @param id
     * @return
     */
    @Override
    public List<ActionProejctDTO> getActionProDtos(String id) {
        List<ActionProjectPO> actionProjectPOS;
        List<ActionProejctDTO> actionProejctDTOS = new ArrayList<>();
        actionProjectPOS = actionProDAO.getActionProDtos(id);
        if (actionProjectPOS != null && actionProjectPOS.size() > 0) {
            for (ActionProjectPO po : actionProjectPOS) {
                Map<String, String> map = new HashMap<>();
                map.put("actionId", po.getId());
                String jzqk = progressReportDAO.getProgressReportdtosNoLimitLimitOne(map);
                ActionProejctDTO dto = new ActionProejctDTO();
                BeanUtils.copyProperties(po, dto);
                List<OrganizationPO> spicFirstPos = null;
                List<OrganizationPO> spicSecPos = null;
                String firstName = "";
                String secName = "";
                if (po.getLevel() != null && !po.getLevel().equals("3")) {
                    if (po.getSpicFirstDeptId() != null) {
                        spicFirstPos = organizationDAO.getOrganizations(po.getSpicFirstDeptId().split(","));
                    }
                    if (po.getSpicSecDeptId() != null) {
                        spicSecPos = organizationDAO.getOrganizations(po.getSpicSecDeptId().split(","));
                    }
                    if (spicFirstPos != null && spicFirstPos.size() > 0) {
                        for (int f = 0; f < spicFirstPos.size(); f++) {
                            String aaa = "";
                            if (f == spicFirstPos.size() - 1) {
                                aaa = spicFirstPos.get(f).getName();
                            } else {
                                aaa = spicFirstPos.get(f).getName().concat(",");
                            }
                            firstName = firstName.concat(aaa);
                        }
                        dto.setSpicFirstDeptName(firstName);
                    }
                    if (spicSecPos != null && spicSecPos.size() > 0) {
                        for (int s = 0; s < spicSecPos.size(); s++) {
                            String aaa = "";
                            if (s == spicSecPos.size() - 1) {
                                aaa = spicSecPos.get(s).getName();
                            } else {
                                aaa = spicSecPos.get(s).getName().concat(",");
                            }
                            secName = secName.concat(aaa);
                        }
                        dto.setSpicSecDeptName(secName);
                    }

                } else if (po.getLevel() != null && !po.getLevel().equals("4")) {
                    dto.setSpicFirstDeptName(po.getSpicFirstDeptId());
                    dto.setSpicSecDeptName(po.getSpicSecDeptId());
                }
                dto.setDef5(jzqk);
                dto.setMainMeasure(po.getMainMeasure().replaceAll("\n", ""));
                actionProejctDTOS.add(dto);
            }
        }
        return actionProejctDTOS;
    }

    @Override
    public List<ActionProejctDTO> getAllAction() {
        List<ActionProjectPO> actionProjectPOS = actionProDAO.getAllAction();
        List<ActionProejctDTO> actionProejctDTOS = new ArrayList<>();
        for (ActionProjectPO po : actionProjectPOS) {
            ActionProejctDTO dto = new ActionProejctDTO();
            BeanUtils.copyProperties(po, dto);
            actionProejctDTOS.add(dto);
        }
        return actionProejctDTOS;
    }

    /**
     * 通过九大项id获取对应行动项
     *
     * @param specialId
     * @return
     */
    @Override
    public List<ActionProejctDTO> getActionProDtosByNineId(String specialId) {
        List<ActionProjectPO> actionProjectPOS;
        List<ActionProejctDTO> actionProejctDTOS = new ArrayList<>();
        try {
            actionProjectPOS = actionProDAO.getActionProDtosByNineId(specialId);
            if (actionProjectPOS != null && actionProjectPOS.size() > 0) {
                for (ActionProjectPO po : actionProjectPOS) {
                    ActionProejctDTO dto = new ActionProejctDTO();
                    BeanUtils.copyProperties(po, dto);
                    List<OrganizationPO> spicFirstPos = null;
                    List<OrganizationPO> spicSecPos = null;
                    String firstName = "";
                    String secName = "";
                    if (po.getSpicFirstDeptId() != null) {
                        spicFirstPos = organizationDAO.getOrganizations(po.getSpicFirstDeptId().split(","));
                        for (int f = 0; f < spicFirstPos.size(); f++) {
                            String aaa = "";
                            if (f == spicFirstPos.size() - 1) {
                                aaa = spicFirstPos.get(f).getName();
                            } else {
                                aaa = spicFirstPos.get(f).getName().concat(",");
                            }
                            firstName = firstName.concat(aaa);
                        }
                        dto.setSpicFirstDeptName(firstName);

                    }
                    if (po.getSpicSecDeptId() != null) {
                        spicSecPos = organizationDAO.getOrganizations(po.getSpicSecDeptId().split(","));
                        for (int s = 0; s < spicSecPos.size(); s++) {
                            String aaa = "";
                            if (s == spicSecPos.size() - 1) {
                                aaa = spicSecPos.get(s).getName();
                            } else {
                                aaa = spicSecPos.get(s).getName().concat(",");
                            }
                            secName = secName.concat(aaa);
                        }
                        dto.setSpicSecDeptName(secName);

                    }
                    actionProejctDTOS.add(dto);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return actionProejctDTOS;
    }

    @Override
    public List<ActionProejctDTO> getActionDetailByParentId(String parentId) {
        List<ActionProjectPO> list = actionProDAO.getActionDetailByParentId(parentId);
        List<ActionProejctDTO> actionProejctDTOS = new ArrayList<>();
        List<OrganizationPO> spicSecPos = null;
        List<OrganizationPO> spicFirstPos = null;
        for (ActionProjectPO po : list) {
            ActionProejctDTO dto = new ActionProejctDTO();
            BeanUtils.copyProperties(po, dto);
            if (po.getSpicFirstDeptId() != null) {
                spicFirstPos = organizationDAO.getOrganizations(po.getSpicFirstDeptId().split(","));
            }
            if (po.getSpicSecDeptId() != null) {
                spicSecPos = organizationDAO.getOrganizations(po.getSpicSecDeptId().split(","));
            }
            String firstName = "";
            String secName = "";
            for (int f = 0; f < spicFirstPos.size(); f++) {
                String aaa = "";
                if (f == spicFirstPos.size() - 1) {
                    aaa = spicFirstPos.get(f).getName();
                } else {
                    aaa = spicFirstPos.get(f).getName().concat(",");
                }
                firstName = firstName.concat(aaa);
            }
            for (int s = 0; s < spicSecPos.size(); s++) {
                String aaa = "";
                if (s == spicSecPos.size() - 1) {
                    aaa = spicSecPos.get(s).getName();
                } else {
                    aaa = spicSecPos.get(s).getName().concat(",");
                }
                secName = secName.concat(aaa);
            }
            dto.setSpicFirstDeptName(firstName);
            dto.setSpicSecDeptName(secName);
            actionProejctDTOS.add(dto);
        }
        return actionProejctDTOS;
    }


    /**
     * 获取行动项下 明细项数量
     *
     * @param actionId
     * @return
     */
    public Long getCountByActionId(String actionId) {
        Long actionDetailNum = actionProDAO.getCountByActionId(actionId);
        if (actionDetailNum == null || actionDetailNum == 0) {
            actionDetailNum = 1L;
        }
        return actionDetailNum;
    }

    /**
     * 获取九大项下 行动项数量
     *
     * @param specialId
     * @return
     */
    public Long getCountBySpecialId(String specialId) {
        Long actionNum = actionProDAO.getCountBySpecialId(specialId);
        if (actionNum == null || actionNum == 0) {
            actionNum = 1L;
        }
        return actionNum;
    }

//    @Override
//    public ActionProejctDTO save(ActionProejctDTO actionProejct) {
//        ActionProjectPO actionProejctPO = new ActionProjectPO();
//        ActionProejctDTO newDto = new ActionProejctDTO();
//        try{
//            BeanUtils.copyProperties(actionProejct, actionProejctPO);
//            actionProejctPO.setId(actionProejct.getId());
//            actionProejctPO.setStartTime(actionProejct.getStartTime());
//            actionProejctPO.setEndTime(actionProejct.getEndTime());
//            ActionProjectPO po =  actionProService.save(actionProejctPO);
//            BeanUtils.copyProperties(po, newDto);
//            }catch(Exception e){
//              e.printStackTrace();
//        }
//        return newDto;
//    }

//    @Override
//    public int deleteById(String id) {
//        return actionProService.delete(id);
//    }

    @Override
    public List<ActionProejctDTO> getActionProByUser(Map<String, Object> map) {
        List<ActionProjectPO> pos = null;
        List<String> parentProjectCode = null;
        String[] arrays = null;
        if (map.get("userId") != null && map.get("userId").equals("1")) {
            parentProjectCode = actionProDAO.getActionProInUser(map);
            arrays = new String[parentProjectCode.size()];
            arrays = parentProjectCode.toArray(arrays);
            pos = actionProDAO.getActionProInIds(arrays);
        } else {
            parentProjectCode = actionProDAO.getActionProNotInUser(map);
            arrays = new String[parentProjectCode.size()];
            arrays = parentProjectCode.toArray(arrays);
            pos = actionProDAO.getActionProInIds(arrays);
        }
        List<ActionProejctDTO> dtos = new ArrayList<>();
        for (ActionProjectPO po : pos) {
            ActionProejctDTO dto = new ActionProejctDTO();
            BeanUtils.copyProperties(po, dto);
            List<OrganizationPO> spicFirstPos = organizationDAO.getOrganizations(po.getSpicFirstDeptId().split(","));
            List<OrganizationPO> spicSecPos = organizationDAO.getOrganizations(po.getSpicSecDeptId().split(","));
            String firstName = "";
            String secName = "";
            for (int f = 0; f < spicFirstPos.size(); f++) {
                String aaa = "";
                if (f == spicFirstPos.size() - 1) {
                    aaa = spicFirstPos.get(f).getName();
                } else {
                    aaa = spicFirstPos.get(f).getName().concat(",");
                }
                firstName = firstName.concat(aaa);
            }
            for (int s = 0; s < spicSecPos.size(); s++) {
                String aaa = "";
                if (s == spicSecPos.size() - 1) {
                    aaa = spicSecPos.get(s).getName();
                } else {
                    aaa = spicSecPos.get(s).getName().concat(",");
                }
                secName = secName.concat(aaa);
            }
            dto.setSpicFirstDeptName(firstName);
            dto.setSpicSecDeptName(secName);
//            dto.setBenchmarkObjId(abc);
            dtos.add(dto);
        }
        return dtos;
    }

    @Override
    public List<ActionProejctDTO> getActionProInIds(String[] ids) {
        List<ActionProjectPO> dtos = actionProDAO.getActionProInIds(ids);
        List<ActionProejctDTO> newDtos = new ArrayList<>();
        for (ActionProjectPO po : dtos) {
            ActionProejctDTO dto = new ActionProejctDTO();
            BeanUtils.copyProperties(po, dto);
            newDtos.add(dto);
        }
        return newDtos;
    }

    @Override
    public List<ActionProejctDTO> getEJDWActionProByUser(Map<String, Object> map) {
        List<String> parentProjectCode = null;
        List<ActionProjectPO> pos = null;
        String orgCode = (String) map.get("orgCode");
        List<OrganizationPO> orgs = organizationDAO.getChildRenOrg(orgCode);
        String[] childRenOrgs = new String[orgs.size()];

        for (int a = 0; a < orgs.size(); a++) {
            childRenOrgs[a] = orgs.get(a).getCode();
        }
        if (map.get("userId") != null && map.get("userId").equals("1")) {
            parentProjectCode = actionProDAO.getEJDWActionProInUser(childRenOrgs);

        } else {
            parentProjectCode = actionProDAO.getEJDWActionProNotInUser(childRenOrgs);
        }
        List<ActionProejctDTO> dtos = new ArrayList<>();
        for (ActionProjectPO po : pos) {
            ActionProejctDTO dto = new ActionProejctDTO();
            BeanUtils.copyProperties(po, dto);
            dtos.add(dto);
        }
        return dtos;
    }

    @Override
    public List<ActionProejctDTO> getActionDetailAndLevelList(String parentActionId, String level) {
        Map<String, String> map = new HashMap<>();
        map.put("parentProjectId", parentActionId);
        map.put("level", level);

        List<ActionProjectPO> actionProjectPOS = actionProDAO.getSpicGroupCode(map);
        List<ActionProejctDTO> dtos = new ArrayList<>();
        actionProjectPOS.stream().forEach(actionProjectPO -> {
            ActionProejctDTO dto = new ActionProejctDTO();
            BeanUtils.copyProperties(actionProjectPO, dto);
            dtos.add(dto);
        });
        return dtos;
    }

    @Override
    public int countByExample(String specialId, String level) {
        MoreActionProjectExample example = new MoreActionProjectExample();
        MoreActionProjectExample.Criteria criteria = example.createCriteria();
        criteria.andLevelEqualTo(level);
        criteria.andSpecialIdEqualTo(specialId);
        return actionProDAO.countByExample(example);
    }

    @Override
    public List<ActionProejctDTO> selectActionByAnyCondition(MoreActionProjectExample example) {
        List<ActionProjectPO> pos = actionProDAO.generateProgress(example);
        List<ActionProejctDTO> dtos = new ArrayList<>();
        pos.stream().forEach(actionProjectPO -> {
            ActionProejctDTO dto = new ActionProejctDTO();
            BeanUtils.copyProperties(actionProjectPO, dto);
            dtos.add(dto);
        });
        return dtos;
    }

    @Override
    public List<ActionProejctDTO> selectActionByOrCondition(Map<String, Object> map) {
        List<ActionProjectPO> pos = actionProDAO.selectActionByOrCondition(map);
        List<ActionProejctDTO> dtos = new ArrayList<>();
        pos.stream().forEach(actionProjectPO -> {
            ActionProejctDTO dto = new ActionProejctDTO();
            BeanUtils.copyProperties(actionProjectPO, dto);
            dtos.add(dto);
        });
        return dtos;
    }

    @Override
    public List<SpecialComRateDTO> selectSpecialComRate(String specialId) {
        List<SpecialComRatePO> specialComRatePOS = actionProDAO.selectSpecialComRate(specialId);
        List<SpecialComRateDTO> dtos = new ArrayList<>();
        for (SpecialComRatePO po : specialComRatePOS) {
            SpecialComRateDTO dto = new SpecialComRateDTO();
            BeanUtils.copyProperties(po, dto);
            dtos.add(dto);
        }
        return dtos;
    }

    @Override
    public List<SpecialCountDTO> getEverySpecilCount() {
        List<SpecialCountPO> pos = actionProDAO.getEverySpecilCount();
        List<SpecialCountDTO> dtos = new ArrayList<>();
        for (SpecialCountPO po : pos) {
            SpecialCountDTO dto = new SpecialCountDTO();
            BeanUtils.copyProperties(po, dto);
            dtos.add(dto);
        }
        return dtos;
    }

    @Override
    public List<SpecialCountDTO> getEveryNOCount() {
        List<SpecialCountPO> pos = actionProDAO.getEveryNOCount();
        List<SpecialCountDTO> dtos = new ArrayList<>();
        for (SpecialCountPO po : pos) {
            SpecialCountDTO dto = new SpecialCountDTO();
            BeanUtils.copyProperties(po, dto);
            dtos.add(dto);
        }
        return dtos;
    }

    @Override
    public List<ThreeActionDTO> getThreeAction(String level) {
        List<ThreeActionPO> pos = actionProDAO.getThreeAction(level);
        List<ThreeActionDTO> dtos = new ArrayList<>();
        for (ThreeActionPO po : pos) {
            ThreeActionDTO dto = new ThreeActionDTO();
            BeanUtils.copyProperties(po, dto);
            dtos.add(dto);
        }
        return dtos;
    }

    @Override
    public List<AllThreeAndFourActionDTO> allThreeAndFourAction(Map<String, Object> map) {
        List<AllThreeAndFourActionPO> pos = actionProDAO.allThreeAndFourAction(map);
        List<AllThreeAndFourActionDTO> dtos = new ArrayList<>();
        for (AllThreeAndFourActionPO po : pos) {
            AllThreeAndFourActionDTO dto = new AllThreeAndFourActionDTO();
            BeanUtils.copyProperties(po, dto);
            dtos.add(dto);
        }
        return dtos;
    }

    @Override
    public List<AllThreeAndFourActionDTO> allLJThreeAndFourAction(Map<String, Object> map) {
        List<AllThreeAndFourActionPO> pos = actionProDAO.allLJThreeAndFourAction(map);
        List<AllThreeAndFourActionDTO> dtos = new ArrayList<>();
        for (AllThreeAndFourActionPO po : pos) {
            AllThreeAndFourActionDTO dto = new AllThreeAndFourActionDTO();
            BeanUtils.copyProperties(po, dto);
            dtos.add(dto);
        }
        return dtos;
    }

    @Override
    public List<SecGroupWorkComDTO> secGroupWorkExecution(Map<String, Object> map) {
        List<SecGroupWorkComPO> pos = actionProDAO.secGroupWorkExecution(map);
        List<SecGroupWorkComDTO> dtos = new ArrayList<>();
        for (SecGroupWorkComPO po : pos) {
            SecGroupWorkComDTO dto = new SecGroupWorkComDTO();
            BeanUtils.copyProperties(po, dto);
            dtos.add(dto);
        }
        return dtos;
    }

    @Override
    public SecGroupMaxMinAndAllDTO getSecGroupMaxMinAndAll(String level) {
        SecGroupMaxMinAndAllPO po = actionProDAO.getSecGroupMaxMinAndAll(level);
        SecGroupMaxMinAndAllDTO dto = new SecGroupMaxMinAndAllDTO();
        BeanUtils.copyProperties(po, dto);
        return dto;
    }

    @Override
    public List<String> getAllSecGroupCode() {
        return actionProDAO.getAllSecGroupCode();
    }

    @Override
    public List<DeptAndOrgActionDTO> getdeptAndOrgAction(Map<String, String> map) {
        List<DeptAndOrgActionPO> pos = actionProDAO.getdeptAndOrgAction(map);
        List<DeptAndOrgActionDTO> dtos = new ArrayList<>();
        for (DeptAndOrgActionPO po : pos) {
            DeptAndOrgActionDTO dto = new DeptAndOrgActionDTO();
            BeanUtils.copyProperties(po, dto);
            dtos.add(dto);
        }
        return dtos;
    }

    @Override
    public int updateByExampleSelective(ActionProejctDTO actionProejctDTO) {
        ActionProjectPO actionProjectPO = new ActionProjectPO();
        BeanUtils.copyProperties(actionProejctDTO, actionProjectPO);
        MoreActionProjectExample moreActionProjectExample = new MoreActionProjectExample();
        MoreActionProjectExample.Criteria criteria = moreActionProjectExample.createCriteria();
        criteria.andIdEqualTo(actionProejctDTO.getId());
        return actionProDAO.updateByExampleSelective(actionProjectPO, moreActionProjectExample);
    }

    @Override
    public List<ExportExcelDTO> exportExcelLists(Map<String, String> map) {
        List<ExportExcelPO> pos = actionProDAO.exportExcelLists(map);
        List<ExportExcelDTO> dtos = new ArrayList<>();
        for (ExportExcelPO po : pos) {
            ExportExcelDTO dto = new ExportExcelDTO();
            BeanUtils.copyProperties(po, dto);
            dtos.add(dto);
        }
        return dtos;
    }

    @Override
    public String getGroupConcatSpecialName(String actionId) {
        return actionProDAO.getGroupConcatSpecialName(actionId);
    }

    @Override
    public int updSpicNameAndSecName(Map<String, String> map) {
        return actionProDAO.updSpicNameAndSecName(map);
    }
}
